using UnityEngine;

[AddComponentMenu("MyGame/Enemy")]
public class Enemy : MonoBehaviour
{

    public float m_speed = 1;
    public float m_life = 10;
    public int m_point = 10;

    protected float m_rotSpeed = 30;
    protected float m_timer = 1.5f;

    protected Transform m_transform;
    
    public AudioClip m_shootClip;
    protected AudioSource m_audio;
    public Transform m_explosionFX;

    // Use this for initialization
    void Start()
    {
		m_transform = this.transform;
        m_audio = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        UpdateMove();
    }

    protected virtual void UpdateMove()
    {
        m_timer -= Time.deltaTime;
        if (m_timer <= 0)
        {
            m_timer = 3;
            m_rotSpeed = -m_rotSpeed;
        }
        m_transform.Rotate(Vector3.up, m_rotSpeed * Time.deltaTime, Space.World);
        m_transform.Translate(new Vector3(0, 0, -m_speed * Time.deltaTime));
    }

    void OnTriggerEnter(Collider other)
    {
        Debug.Log("enemy trigger:" + other.tag);
        if (string.Compare(other.tag, "PlayerRocket", System.StringComparison.Ordinal) == 0)
        {
            Rocket rocket = other.GetComponent<Rocket>();
            if (rocket != null)
            {
                m_life -= rocket.m_power;
                if (m_life <= 0)
                {
                    GameManager.instance.AddScore(m_point);

                    Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);
                    Destroy(this.gameObject);
                }
            }
        }
        else if (string.Compare(other.tag, "Player", System.StringComparison.Ordinal) == 0 || string.Compare(other.tag, "Bound", System.StringComparison.Ordinal) == 0)
        {
            m_life = 0;
            Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);
            Destroy(this.gameObject);
        }
    }
}
